//
//  CourseOneViewController.m
//  IhuayuanClient
//
//  Created by 黄思清 on 2017/7/3.
//  Copyright © 2017年 com.ihuayuan. All rights reserved.
//

#import "CourseOneViewController.h"
#import "CourseOneModel.h"
#import "OneTableViewCell.h"

@interface CourseOneViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) UITableView *tableView;
@property(nonatomic,strong) CourseOneModel *mode;

@end


@implementation CourseOneViewController

-(CourseOneModel *)mode
{
    if (!_mode) {
        _mode = [[CourseOneModel alloc]init];
    }
    return _mode;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Request];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT-200) style:UITableViewStylePlain];
    _tableView.delegate   = self;
    _tableView.dataSource = self;
    _tableView.rowHeight  = 50;
    _tableView.sectionHeaderHeight = 0;
    _tableView.showsVerticalScrollIndicator = false;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[OneTableViewCell class] forCellReuseIdentifier:@"onecell"];


    
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    switch (indexPath.row) {
        case 0:
            [cell.titL setText:@"学生姓名"];
            [cell.conL setText:self.mode.name];
            break;
        case 1:
            [cell.titL setText:@"入班时间"];
            [cell.conL setText:self.mode.join_date];
            break;
        case 2:
            [cell.titL setText:@"总课次"];
            [cell.conL setText:self.mode.classhour_total];
            break;
        case 3:
            [cell.titL setText:@"购买课次"];
            [cell.conL setText:self.mode.payclasshour];
            break;
        case 4:
            [cell.titL setText:@"赠送课次"];
            // 腰间

            [cell.conL setText:[NSString stringWithFormat:@"%d",[self.mode.classhour_total intValue]-[self.mode.payclasshour intValue]]];
            break;
        case 5:
            [cell.titL setText:@"已用课次"];
            [cell.conL setText:[NSString stringWithFormat:@"%d", [self.mode.classhour_total intValue]-[self.mode.classhour_last intValue]-[self.mode.classhour_give intValue]]];
            break;
        case 6:
            [cell.titL setText:@"剩余课次"];
            [cell.conL setText:[NSString stringWithFormat:@"%d",[self.mode.classhour_last intValue]+[self.mode.classhour_give intValue]]];
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCourse&a=courseDetailbasic" parmDic:@{@"aid":self.aid} requestType:RequestGET Success:^(id data)
    {
        self.mode = [CourseOneModel modeConfigure:data];
        NSLog(@"%@",self.mode);
        [_tableView reloadData];
        [self CreateBottomV];

    } error:^(NSError *error) {

    }];
}
-(void)CreateBottomV
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 20, MAIN_SCREEN_WIDTH, 240)];
    [footerView setBackgroundColor:[UIColor whiteColor]];

    float main_width;
    main_width = MAIN_SCREEN_WIDTH - 20;
    int i;
    for (i=1; i<=[self.mode.classhour_total intValue]; i++) {

        float y = 0;
        int x = 0;

        if(i<=10){
            x = i;
            y = 22;
        }else if(i<=20 && i>10){
            x = i - 10;
            y = 24 + main_width/10;
        }else if(i<=30 && i>20){
            x = i - 20;
            y = 26 + main_width/10*2;
        }else if(i<=40 && i>30){
            x = i -30;
            y = 28 + main_width/10*3;
        }else if(i<=50 && i>40){
            x = i - 40;
            y = 32 + main_width/10*4;
        }else if(i<=60 && i>50){
            x = i - 50;
            y = 36 + main_width/10*5;
        }else if(i<=70 && i>60){
            x = i - 60;
            y = 40 + main_width/10*6;
        }


        UIImageView *cView = [[UIImageView alloc]initWithFrame:CGRectMake((x-1)*(main_width/10)+1, y, main_width/10-2, main_width/10-2)];

        // 已经使用的课次数
        int useClass = [self.mode.classhour_total intValue]-[self.mode.classhour_last intValue]-[self.mode.classhour_give intValue];

        // 实际购买的课次数
        int payClass = [self.mode.payclasshour intValue];
        

        // 如果已经使用的课次数大于购买的 那么购买的全部使用了
        if (useClass>payClass)
        {
            if (i<=payClass) {
                [cView setImage:[UIImage imageNamed:@"buy_01"]];
            }else if (i>payClass && i <= useClass)
            {
                [cView setImage:[UIImage imageNamed:@"buy_04"]];
            }else if (i>useClass)
            {
                [cView setImage:[UIImage imageNamed:@"buy_03"]];
            }
        }else if (useClass < payClass) // 使用的课次数小于购买的课次数
        {
            if (i<=useClass) {
                 [cView setImage:[UIImage imageNamed:@"buy_01"]];
            }else if (i >useClass && i<=payClass)
            {
                [cView setImage:[UIImage imageNamed:@"buy_02"]];
            }else
            {
                [cView setImage:[UIImage imageNamed:@"buy_03"]];
            }
        }
        [footerView addSubview:cView];
    }
    _tableView.tableFooterView = footerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
